package com.spring.aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 每一行代码，都是改变世界的力量
 *
 * @author 郭帅
 * @date 2021/11/1 11:29 下午
 */
@Aspect
@Component
public class AspectConfig {

    @Pointcut("execution(* com.spring.aop.service..*(..))")
    public void myPointCut() {

    }
//    @Before("myPointCut()")
//    public void adviceBefore2() {
//        System.out.println("before2 ----------");
//    }

    @Before("myPointCut()")
    public void adviceBefore() {
        System.out.println("before ---------");
    }


    @After("myPointCut()")
    public void adviceAfter() {
        System.out.println("after ----------");
    }

//    @After("myPointCut()")
//    public void adviceAfter2() {
//        System.out.println("after2 ----------");
//    }

    @Around("myPointCut()")
    public Object adviceAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around before ----------");
        Object response = null;
        try {
            response = proceedingJoinPoint.proceed();
        } catch (Exception e) {

        }
        System.out.println("around after ----------");
        return response;
    }
//    @Around("myPointCut()")
//    public Object adviceAround2(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//        System.out.println("around before2 ----------");
//        Object response = proceedingJoinPoint.proceed();
//        System.out.println("around after2 ----------");
//        return response;
//    }

    @AfterReturning("myPointCut()")
    public void adviceAfterReturning() {
        System.out.println("after returning ----------");
    }

    @AfterThrowing("myPointCut()")
    public void adviceAfterThrowing(JoinPoint joinPoint) {
        System.out.println("after throwing ----------");
    }

}
